package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.IForgetPassWordService;
import com.zerdoor.service.ITaskListService;
import com.zerdoor.util.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2015/5/7.
 * 忘记密码 控制层
 */
@Controller
@RequestMapping("/forgetPassWord")
public class ForgetPassWordController {

    @Autowired
    private IForgetPassWordService forgetPassWordService;

    @Autowired
    private Email simpleMail;
    //发送邮箱对象
    @Value("#{mailsenderProperties['mail.from']}")
    private String fromMail;

    /**
     * 跳转到忘记密码首页
     * @param request
     * @return
     */
    @RequestMapping(value = "/forgetPassWord", method = RequestMethod.GET)
    public String taskList(HttpServletRequest request){


        return "/forget/forgetPwd";
    }

    /**
     * 重置密码
     *
     * @param name 账号
     * @param email 邮箱
     * @param phone 手机
     * @return
     */
    @RequestMapping(value = "/resettingPwd", method = RequestMethod.POST)
    public ResponseEntity<?> resettingPwd(@RequestBody Map<String, String> model, HttpServletRequest request) {



        try {

            String name = model.get("name");
            String email = model.get("email");
            String phone = model.get("phone");

            Map<String,String> msgMap = new HashMap<String,String>();

            msgMap.put("status","false");//状态
            boolean boo = true;
            if(StringUtils.isEmpty(name)){

                msgMap.put("errorLabel","userImg");
                msgMap.put("msg","账号不能为空！");
                boo = false;
            }
            if(StringUtils.isEmpty(email) && boo){

                msgMap.put("errorLabel","emailImg");
                msgMap.put("msg","认证邮箱不能为空！");

                boo = false;
            }
            if(StringUtils.isEmpty(phone) && boo){

                msgMap.put("errorLabel","phoneImg");
                msgMap.put("msg","注册手机号不能为空！");

                boo = false;
            }

            if(boo){
                msgMap = forgetPassWordService.updatePwdByUserInfo(name,email,phone,simpleMail,fromMail);
            }

            ResponseEntity<Map<String,String>> responseEntity = new ResponseEntity<Map<String,String>>(msgMap, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }





}
